<?php
class ControllerDelimanHanhdong extends Controller
{
	private $error = array();
	private $chucnangid;
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
	 	$this->load->model("deliman/hanhdong");
		$this->load->model("deliman/chucnang");
		
		$this->chucnangid = $this->request->get['chucnangid'];
		
		$this->data['chucnang'] = $this->model_deliman_chucnang->getItem($this->chucnangid);
   	}
	
	public function index()
	{
		if($this->chucnangid == "")
			$this->response->redirect("?route=deliman/chucnang");
		$this->getList();
	}
	
	public function insert()
	{
		if(!$this->user->hasPermission($this->getRoute(), "add"))
		{
			$this->response->redirect("?route=common/permission");
		}
		
    	$this->getForm();
	}
	
	public function update()
	{
		if(!$this->user->hasPermission($this->getRoute(), "edit"))
		{
			$this->response->redirect("?route=common/permission");
		}
		else
		{
			//$this->load->language('deliman/hanhdong');
			//$this->data = array_merge($this->data, $this->language->getData());
			
			
			
			$this->data['haspass'] = false;
			$this->data['readonly'] = 'readonly="readonly"';
			$this->data['class'] = 'readonly';
		
			$this->getForm();
		}
		
  	}
	
	public function delete() 
	{
		$listhanhdongid=$this->request->post['delete'];
		if(count($listhanhdongid))
		{
			foreach($listhanhdongid as $hanhdongid)
			{
				$this->model_deliman_hanhdong->delete($hanhdongid);	
			}
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('deliman/hanhdong/insert&chucnangid='.$this->chucnangid);
		$this->data['delete'] = $this->url->http('deliman/hanhdong/delete');
		$this->load->model("deliman/chucnang");		
		$this->data['datas'] = array();
		$where = "";
		if($this->chucnangid)
		{
			$where.=" AND chucnangid = '".$this->chucnangid."'";
		}
		$rows = $this->model_deliman_hanhdong->getList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/hanhdong/update&hanhdongid='.$this->data['datas'][$i]['hanhdongid'].'&chucnangid='.$this->chucnangid);
			
			$this->data['datas'][$i]['text_edit'] = "Edit";
			$chucnang = $this->model_deliman_chucnang->getItem($this->data['datas'][$i]['chucnangid']);
			$this->data['datas'][$i]['tenchucnang'] = $chucnang['tenchucnang'];
		}
		
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/hanhdong_list.tpl";
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	
	private function getForm()
	{
		//load cb chuc nang
		$this->load->model('deliman/chucnang');
		$this->data['chucnangs'] = $this->model_deliman_chucnang->getList();		
		
		if ((isset($this->request->get['hanhdongid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_hanhdong->getItem($this->request->get['hanhdongid']);
    	}
		else
		{
			$this->data['item']['chucnangid'] = $this->chucnangid;
			$this->data['item']['mahanhdong'] = $this->data['chucnang']['machucnang']."-";
		}
		$this->id='content';
		$this->template='deliman/hanhdong_form.tpl';
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	public function save()
	{
		$data = $this->request->post;
		if($this->validateForm($data))
		{
			if($data['hanhdongid']=="")
			{
				$this->model_deliman_hanhdong->insert($data);	
			}
			else
			{
				$this->model_deliman_hanhdong->update($data);	
			}
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		if (trim($data['mahanhdong']) == "") 
		{
      		$this->error['mahanhdong'] = "Bạn chưa nhập mã hành động";
    	}
		else
		{
			$where = " AND mahanhdong = '".$data['mahanhdong']."'";
			$kybaos = $this->model_deliman_hanhdong->getList($where);
			if(count($kybaos) > 0 && $data['hanhdongid'] == "")
			{
				$this->error['mahanhdong'] = "mã hành động đã tồn tại trong hệ thống";	
			}
		}
		
		if (trim($data['tenhanhdong']) == "") 
		{
      		$this->error['tenhanhdong'] = "Bạn chưa nhập tên hành động";
    	}
		
		if (trim($data['chucnangid']) == "")
		{
			$this->error['chucnangid'] = "Bạn chưa chọn tên chức năng";
		}
	
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	
}
?>